from Base import *
from Header import *

class CAppFlagElement(CInfoElement):
    _gName = "App Flag Element"
    _gNameAppFlagACNcall = "ACNcall"
    _gNameAppFlagEcall = "Ecall"
    _gNameAppFlagSvl = "SVL"
    _gNameAppFlagAlm = "Alarm"
    _gNameAppFlagRdc = "Remote Door Contorl"
    _gNameAppFlagGeo = "GEO FENCE"
    _gNameAppFlagSpd = "Speed Alert"
    _gNameAppFlagMil = "MIL"
    _gNameAppFlagBcall = "Road Side"
    _gNameAppFlagConSearch = "Connected Search"
    _gNameAppFlagDD = "Destination Download"
    _gNameAppFlagMan = "Maintenance"

    _gEleLength = 2

    def __init__(self, name=_gName):
        super(CAppFlagElement, self).__init__(name)
        self._hlColor = 'black_on_yellow'

    def InitSubItem(self):
        name = self._gNameAppFlagACNcall
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagEcall
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagSvl
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagAlm
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagRdc
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)


        name = self._gNameAppFlagGeo
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagSpd
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagMil
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagBcall
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagConSearch
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagDD
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

        name = self._gNameAppFlagMan
        item = CItem(name)
        self._subList[name] = item
        self._nameList.append(name)

    def Parse(self, dataStr):
        self.InitSubItem()

        self._startIndex = dataStr.GetIndex()

        byte1 = dataStr.GetByte()
        byte2 = dataStr.GetByte()

        afACNcall = (byte1 & 0x80) >> 7
        item = self._subList[self._gNameAppFlagACNcall]
        item._rawStr = "%d" % afACNcall

        afEcall = (byte1 & 0x40) >> 6
        item = self._subList[self._gNameAppFlagEcall]
        item._rawStr = "%d" % afEcall

        afSvl = (byte1 & 0x20) >> 5
        item = self._subList[self._gNameAppFlagSvl]
        item._rawStr = "%d" % afSvl

        afAlm = (byte1 & 0x10) >> 4
        item = self._subList[self._gNameAppFlagAlm]
        item._rawStr = "%d" % afAlm

        afRdc = (byte1 & 0x08) >> 3
        item = self._subList[self._gNameAppFlagRdc]
        item._rawStr = "%d" % afRdc

        afGeo = (byte1 & 0x04) >> 2
        item = self._subList[self._gNameAppFlagGeo]
        item._rawStr = "%d" % afGeo

        afSpd = (byte1 & 0x02) >> 1
        item = self._subList[self._gNameAppFlagSpd]
        item._rawStr = "%d" % afSpd

        afMil = (byte1 & 0x01)
        item = self._subList[self._gNameAppFlagMil]
        item._rawStr = "%d" % afMil

        afRdSide = (byte2 & 0x80) >> 7
        item = self._subList[self._gNameAppFlagBcall]
        item._rawStr = "%d" % afRdSide

        afCS = (byte2 & 0x40) >> 6
        item = self._subList[self._gNameAppFlagConSearch]
        item._rawStr = "%d" % afCS

        afDD = (byte2 & 0x20) >> 5
        item = self._subList[self._gNameAppFlagDD]
        item._rawStr = "%d" % afDD

        afMan = (byte2 & 0x10) >> 4
        item = self._subList[self._gNameAppFlagMan]
        item._rawStr = "%d" % afMan


        self._rawStr = DATA_STRING_FORMAT.format(dataStr.GetString(self._startIndex, self._startIndex + self._gEleLength))

class CStaFlagElement(CAppFlagElement):
    _gName = "Status Flag Element"

    def __init__(self, name=_gName):
        super(CStaFlagElement, self).__init__(name)
        self._hlColor = 'black_on_yellow'
